//
//  TheThirdLoginApi.m
//  LMToothbrush
//
//  Created by maple on 2016/11/30.
//  Copyright © 2016年 maple. All rights reserved.
//

#import "TheThirdLoginApi.h"
#import "GetUserInfoApi.h"    // 获得用户信息api

@implementation TheThirdLoginApi{
    NSDictionary *_dic;
}

-(id)initWithDic:(NSDictionary *)dic{
    if (self = [super init]) {
        _dic = dic;
    }
    return self;
}

- (NSString *)requestUrl {
    return @"new/user/third_login";
}

- (YTKRequestMethod)requestMethod {
    return YTKRequestMethodPOST;
}

- (id)requestArgument {
    NSMutableDictionary *dic = [NSMutableDictionary getPubdicGuest];
    if (_dic[@"openid"]) {
        [dic setObject:_dic[@"openid"] forKey:@"openid"];
    }
    if (_dic[@"type"]) {
        [dic setObject:_dic[@"type"] forKey:@"type"];
        
    }
    NSString *sign=[NSString getSign:dic];
    [dic setObject:sign forKey:@"sign"];
    return dic;
}

- (NSString *)userId {
    return [[[self responseJSONObject] objectForKey:@"userId"] stringValue];
}

//-(void)analyseResponseObject {
//    
//    NSDictionary * dict = [self responseJSONObject];
//    NSLog(@"dict = %@",dict[@"errmsg"]);
//    if ([dict[@"errcode"] intValue] == 0) {
//        
//        [SVProgressHUD showSuccessWithStatus:@"您已安全登录！"];
//        SAVE_OBJECT(_dic[@"openid"], USERNAME);
//        SAVE_OBJECT(_dic[@"openid"], PASSWORD);
//        SAVE_OBJECT(dict[@"data"][@"uid"], UID);
//        SAVE_OBJECT(dict[@"data"][@"token"], TOKEN);
//        SAVE_OBJECT(@"YES", LOGINING);   // 登录状态保存来设置根控制器
//        NSLog(@"login = %@",EXRACT_OBJECT(LOGINING));
//        [self getUserBindDevice];
//        
//    } else {
//        [SVProgressHUD showErrorWithStatus:dict[@"errmsg"]];
//    }
//}

//- (void)getUserBindDevice
//{
//    GetUserBindDeviceApi *api = [[GetUserBindDeviceApi alloc]initWithDictionary:@{@"uid":EXRACT_OBJECT(UID),@"token":EXRACT_OBJECT(TOKEN)}];
//    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
//        NSDictionary *dic = [request responseJSONObject];
//        if ( [dic[@"errcode"] integerValue] == 0) {
//            //跳转页面
//            NSDictionary *dataDic = dic[@"data"][0];
//            SAVE_OBJECT(dataDic[@"mac"], BINDMACADDRESS);
//            SAVE_OBJECT(dataDic[@"used_day"], USED_DAY);
//            SAVE_OBJECT(dataDic[@"brush_head_day"], BRUSH_HEAD_DAY);
//            NSString *myStr = dataDic[@"mac"];
//            //储存模型
//            for (TieInfo *model in [BluetoothManager Share].blueperihalArray) {
//                if ([model.mac.lowercaseString isEqualToString:myStr.lowercaseString])
//                {
//                    [[LMSaveBindDataManager sharedInstance] createtablae:@"bindModel"];
//                    [[LMSaveBindDataManager sharedInstance]addDevice:model withtablename:@"bindModel"];
//                }
//            }
//            
//            [[AppDelegate shareAPPDelegate] gotoMainView];
//        }else{
//            //跳转页面
//            [[AppDelegate shareAPPDelegate] gotoDeviceView];
//        }
//    } failure:^(YTKBaseRequest *request) {
//        [SVProgressHUD showErrorWithStatus:@"请求失败"];
//    }];
//}

- (void)GetUserInfo {
    
    GetUserInfoApi * userInfoApi = [[GetUserInfoApi alloc] init];
    [userInfoApi startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        [userInfoApi analyseResponseObject];
    } failure:^(YTKBaseRequest *request) {
        [SVProgressHUD showErrorWithStatus:@"获取用户信息失败！"];
    }];
    
}


-(void)dealloc
{
    NSLog(@"thethirdloginApi dealloc");
}
@end
